package com.example.smartcitydemo.ui.home.fragment

import android.content.Intent
import android.os.Bundle
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import androidx.recyclerview.widget.GridLayoutManager
import com.blankj.utilcode.util.ToastUtils
import com.example.smartcity_jscs.network.ResultStates.Resource
import com.example.smartcitydemo.NEWS_SEARCH
import com.example.smartcitydemo.R

import com.example.smartcitydemo.base.BaseFragment
import com.example.smartcitydemo.bean.HomeBannerBean
import com.example.smartcitydemo.bean.ServiceBean
import com.example.smartcitydemo.databinding.FragmentHomeBinding
import com.example.smartcitydemo.ext.init

import com.example.smartcitydemo.ext.onLoad
import com.example.smartcitydemo.ui.home.MyServiceAdapter
import com.example.smartcitydemo.ui.home.MyViewPager2Adapter
import com.example.smartcitydemo.ui.home.activity.SearchResultActivity
import com.example.smartcitydemo.ui.job.JobActivity
import com.example.smartcitydemo.ui.smartbus.SmartBusActivity
import com.example.smartcitydemo.ui.subway.SubWayActivity
import com.example.smartcitydemo.viewModel.home.HomeViewModel
import com.youth.banner.adapter.BannerImageAdapter
import com.youth.banner.holder.BannerImageHolder
import java.util.*

class HomeFragment : BaseFragment<FragmentHomeBinding>(R.layout.fragment_home) {
    private val homeViewModel: HomeViewModel by activityViewModels()

    private val serviceAdapter: MyServiceAdapter by lazy(LazyThreadSafetyMode.SYNCHRONIZED) {
        MyServiceAdapter()
    }

    override fun initData() {
        homeViewModel.getNewsType()
        homeViewModel.getHomeBanner()
        homeViewModel.getServiceTypeData()

    }

    override fun initView(bindView: FragmentHomeBinding, savedInstanceState: Bundle?) {
        bindView.run {
            myBanner.let {
                it.setBannerGalleryMZ(15)
            }
            serviceRecycler.run {
                layoutManager = GridLayoutManager(context, 5)
                adapter = serviceAdapter.apply {
                    setEmptyView(R.layout.loading_layout)
                    setDiffCallback(ServiceBean.diffCallback)
                    setOnItemClickListener { adapter, view, pos ->
                        when (pos) {
                            //城市地铁
                            1 -> {
                                val intent =
                                    Intent(this@HomeFragment.context, SubWayActivity::class.java)
                                startActivity(intent)
                            }
                            //找工作
                            9-> {
                                val intent =
                                    Intent(this@HomeFragment.context, JobActivity::class.java)
                                startActivity(intent)
                            }
                            //智慧巴士
                            2->{
                                val intent =
                                    Intent(this@HomeFragment.context, SmartBusActivity::class.java)
                                startActivity(intent)
                            }


                        }
                    }
                }
            }
            homePageTypeBar.typeBarText.text = "服务类型"
            editTextSearch.setOnEditorActionListener { textView, actionId, event ->
                val toString = textView.text.toString()
                if (toString.isEmpty()) {
                    ToastUtils.showShort("请输入内容")
                } else {
                    val intent = Intent(
                        this@HomeFragment.requireActivity(),
                        SearchResultActivity::class.java
                    ).apply {
                        putExtra(NEWS_SEARCH, toString)
                    }
                    startActivity(intent)
                }
                true
            }
        }
    }

    override fun addObservers(bindView: FragmentHomeBinding) {
        homeViewModel.newsTypeLiveData.observe(viewLifecycleOwner) {
            when (it) {
                is Resource.Success -> {
                    val fragmentList: MutableList<Fragment> = mutableListOf()
                    val tabList: MutableList<String> = mutableListOf()
                    it.data!!.forEach { it1 ->
                        val newInstance = HomeNewsFragment.newInstance(it1.id)
                        fragmentList.add(newInstance)
                        tabList.add(it1.name)
                    }
                    val adapter: MyViewPager2Adapter = MyViewPager2Adapter(
                        this@HomeFragment, fragmentList
                    )
                    bindView.run {
                        mypager2.init(adapter, myTab) { tab, pos ->
                            tab.text = tabList[pos]
                        }
                        mypager2.offscreenPageLimit = fragmentList.size
                    }
                }

            }
        }
        homeViewModel.homeBannerBean.observe(viewLifecycleOwner) {
            when (it) {
                is Resource.Success ->
                    bindView.myBanner.run {
                        setAdapter(object : BannerImageAdapter<HomeBannerBean>(it.data!!) {
                            override fun onBindView(
                                holder: BannerImageHolder?,
                                data: HomeBannerBean?,
                                position: Int,
                                size: Int
                            ) {

                                holder!!.imageView.run {
                                    onLoad(data?.advImg)
                                    tag = data?.id ?: 1
                                }
                            }

                        })
                    }
            }
        }

        homeViewModel.ServiceTypeData.observe(viewLifecycleOwner) {
            when (it) {
                is Resource.Success ->
                    serviceAdapter.setDiffNewData(it.data!!.toMutableList())
            }
        }
    }

}